define(["jquery"],function($){
	var quest = {};
	
	/**
	 * 数据处理(若不需要数据处理，可以不写)
	 */
    quest.formatData = function(obj){
    	for (var i = 0; i < obj.list.length; i++) {
    		if( typeof obj.list[i] == "string" ){
    			var str = obj.list[i];
    			obj.list[i] = {};
    			obj.list[i].key = str;
    			obj.list[i].text = str;
    		}
    	}
    	if( obj.relation && typeof obj.relation == "object" && !$.isArray(obj.relation) ){
    		var obj_relation = obj.relation;
    		obj.relation = [];
    		obj.relation.push(obj_relation);
    	}
    	if( obj.relation && $.isArray(obj.relation) ){
    		for (var i = 0; i < obj.list.length; i++) {
    			for (var j = 0; j < obj.relation.length; j++) {
    				if( obj.list[i].key == obj.relation[j].relationKey ){
    					obj.list[i].relationId = obj.relation[j].relationId;
    					break;
    				}
    			}
    		}
    	}
		return obj;
	}

	/**
	 * tpl模板
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_checkboxRelation" type="text/html">'+
				'	<div {{if relationId}}data-relationId="{{relationId}}"{{/if}} class="qaCard qaCard-checkboxRelation form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'   	<div class="control-input">'+
				'			{{if status=="default"}}'+
				'  	 			<div class="checkbox">'+
				'					{{each list as item i}}'+
				'						<label>'+
				'							<input type="checkbox" class="validatebox" name="{{name}}" rId="{{item.relationId}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" {{if dataUnit}}data-dataUnit="{{dataUnit}}"{{/if}} data-validate="{{validate}}" data-attention="{{attention}}">'+
				'							{{item.text}}'+
				'						</label>'+
				'					{{/each}}'+
				'  	 			</div>'+
				'			{{else if status=="readonly"}}'+	
				'  	 			<div class="checkbox disabled">'+
				'					{{each list as item i}}'+
				'						<label>'+
				'							<input type="checkbox" class="validatebox" name="{{name}}" rId="{{item.relationId}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" {{if dataUnit}}data-dataUnit="{{dataUnit}}"{{/if}} data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'							{{item.text}}'+
				'						</label>'+
				'					{{/each}}'+
				'  	 			</div>'+
				'			{{else if status=="disabled"}}'+
				'  	 			<div class="checkbox disabled">'+
				'					{{each list as item i}}'+
				'						<label>'+
				'							<input type="checkbox" class="validatebox" name="{{name}}" rId="{{item.relationId}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" {{if dataUnit}}data-dataUnit="{{dataUnit}}"{{/if}} data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'						</label>'+
				'					{{/each}}'+
				'  	 			</div>'+
				'			{{/if}}'+
				'		</div>'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,value){
		if( value ){
			if( value.quesValue && value.quesValue.indexOf("[") >= 0 && value.quesValue.indexOf("]") >= 0 ) value.quesValue = JSON.parse(value.quesValue);
			if( value.quesValue && $.isArray(value.quesValue) && value.quesValue.length > 0 ){
				for (var i = 0; i < value.quesValue.length; i++) {
					$dom.each(function(j){
						if( value.quesValue[i] == $(this).val() ){
							$(this).attr("checked","checked");
							$(this).trigger("change");
							return true; // 退出本次循环
						}
					});
				}
			}
		}
	}

	/**
	 * 获取value（保存时，获取每个dom对应的答案）
	 */
	quest.getValue = function($dom,name){
		var domArr = $dom.parents(".qaCard").find("input[type='checkbox'][name='" + name + "']:checked");
		var val = [];
		domArr.each(function(){
			val.push( $(this).val() );
		});
		var value = {};
		value.quesValue = JSON.stringify(val);
		value.quesId = $dom.attr("data-quesId");
		value.deCode = $dom.attr("data-deCode");
		value.innDeCode = $dom.attr("data-innDeCode");
		var dataUnit = $dom.attr("data-dataUnit");
		if( dataUnit ) value.dataUnit = dataUnit;
		return value;
	}

	/**
	 * 事件(若没有事件函数，可以不写)
	 */
	quest.event = function(params,id){
		var dom = $("#" + id + " .qaCard[relationId]"); // 表单里带relationId的dom元素
		// 选中某个复选项，某个复选项对应的才显示
		$("#" + id + " .qaCard-checkboxRelation .checkbox input:checkbox").unbind("click");
		$("#" + id + " .qaCard-checkboxRelation .checkbox input:checkbox").data({"dom":dom});
		$("#" + id + " .qaCard-checkboxRelation .checkbox input:checkbox").bind("click",onCheckboxRelation);
	}

	var onCheckboxRelation = function(event){
		var dom = $(this).data("dom");
		var This = $(this);
		var rId = This.attr("rId");
		dom.each(function(j){
			if(dom.eq(j).attr("relationId") == rId){
				if( This.is(':checked') ){
					dom.eq(j).show();
				}else{
					dom.eq(j).hide();
				}
			}
		});
	}

	return quest;
});